package com.csw.android.player.view.video

import android.app.Activity
import android.content.Context
import android.content.pm.ActivityInfo
import android.hardware.SensorManager.SENSOR_DELAY_UI
import android.util.AttributeSet
import android.view.OrientationEventListener
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.Observer
import com.csw.android.dev_utils.ex.ViewEx.gone
import com.csw.android.player.base.AbsMediaControllerView
import com.csw.android.player.base.IMediaPlayer
import com.csw.android.player.base.MediaInfo
import com.csw.android.player.utils.ScreenOrientationUtils
import com.csw.android.player.view.controller.FullScreenMediaControllerView

open class FullScreenVideoView(context: Context, attrs: AttributeSet? = null) :
    VideoView(context, attrs) {

    private val playingMediaListener = Observer<MediaInfo?> {
        mediaInfo = it
    }

    private val orientationEventListener =
        object : OrientationEventListener(context, SENSOR_DELAY_UI) {
            private var hasChanged = false
            private var uiGravity: ScreenOrientationUtils.UIGravity? = null

            override fun onOrientationChanged(orientation: Int) {
                if (orientation < 0) {
                    return
                }
                ScreenOrientationUtils.orientationToUIGravity(orientation).run {
                    if (uiGravity != this) {
                        uiGravity = this
                        handleOrientationChange(this, orientation)
                        hasChanged = true
                    }
                }
            }

            fun handleOrientationChange(
                uiGravity: ScreenOrientationUtils.UIGravity,
                orientation: Int
            ) {
                val activity = context
                if (activity is Activity) {
                    var activityOrientation = activity.requestedOrientation
                    mMediaPlayer?.playerInfo?.run {
                        if (videoWidth > videoHeight) {
                            when {
//                                hasChanged && uiGravity == ScreenOrientationUtils.UIGravity.BOTTOM -> {
//                                    activity.finish()
//                                    return
//                                }

                                else -> {
                                    if (orientation > 180) {
                                        activityOrientation =
                                            ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
                                    } else {
                                        activityOrientation =
                                            ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
                                    }
                                }
                            }
                        } else {
                            activityOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
//                            when (uiGravity) {
//                                ScreenOrientationUtils.UIGravity.BOTTOM -> {
//                                    activityOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
//                                }
//
//                                ScreenOrientationUtils.UIGravity.TOP -> {
//                                    activityOrientation =
//                                        ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT
//                                }
//
//                                ScreenOrientationUtils.UIGravity.LEFT -> {
//                                    activityOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
//                                }
//
//                                ScreenOrientationUtils.UIGravity.RIGHT -> {
//                                    activityOrientation =
//                                        ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
//                                }
//                            }
                        }
                        if (activityOrientation != activity.requestedOrientation) {
                            activity.requestedOrientation = activityOrientation
                        }
                    }
                }
            }
        }

    override fun createMediaController(context: Context): AbsMediaControllerView? {
        return FullScreenMediaControllerView(context)
    }

    override fun onPlayerBind(mediaPlayer: IMediaPlayer) {
        super.onPlayerBind(mediaPlayer)
        mediaPlayer.playList?.currMedia?.observeForever(playingMediaListener)
        if (isPlaying()) {
            mPreviewView.gone()
        }
    }

    override fun onPlayerUnbind(mediaPlayer: IMediaPlayer) {
        mediaPlayer.playList?.currMedia?.removeObserver(playingMediaListener)
        super.onPlayerUnbind(mediaPlayer)
        (context as? Activity)?.finish()
    }

    override fun onResume(owner: LifecycleOwner) {
        super.onResume(owner)
        orientationEventListener.enable()
    }

    override fun onPause(owner: LifecycleOwner) {
        orientationEventListener.disable()
        super.onPause(owner)
    }

}